﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ModbusTCPDLL;

namespace ModbusTCPDLLTest
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            this.Load += FrmMain_Load;
        }
        ModTCP objMod = new ModTCP();
        void FrmMain_Load(object sender, EventArgs e)
        {
            this.cmb_Function.Items.Add("01 Coil Status(0x)");
            this.cmb_Function.Items.Add("02 Input Status(1x)");
            this.cmb_Function.Items.Add("03 Holding Registers(4x)");
            this.cmb_Function.Items.Add("04 Input Registers(3x)");
            this.cmb_Function.SelectedIndex = 0;
           //建立Modbus TCP连接
            if (objMod.Connect("127.0.0.1", "502"))
            {
                this.lbl_state.Text = "连接成功";
            }
            else
            {
                this.lbl_state.Text = "连接出错";
            }
        }

        private void btn_Read_Click(object sender, EventArgs e)
        {
            byte[] Res = null;
            int Start = Convert.ToInt32(this.txt_Address.Text.Trim());
            int Length = Convert.ToInt32(this.txt_Length.Text.Trim());
            switch (this.cmb_Function.SelectedIndex)
            {
                case 0:
                    Res = objMod.ReadOutputStatus(Start, Length);
                    break;
                case 1:
                    break;
                case 2:
                    Res = objMod.ReadKeepReg(Start, Length);
                    break;
                case 3:
                    break;
                default:
                    break;
            }
            this.list_data.Items.Clear();
            if (Res != null && Res.Length > 0)
            {
                for (int i = 0; i < Res.Length; i++)
                {
                    this.list_data.Items.Add(Res[i]);
                }
            }
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            objMod.Disconnect();
        }

        private void btn_Write_Click(object sender, EventArgs e)
        {
            int Address=Convert.ToInt32(this.txt_Address.Text.Trim());
            switch (this.cmb_Function.SelectedIndex)
            {
                case 0:
                    objMod.ForceOnOff(Address, this.txt_Value.Text.Trim());
                    break;
                case 1:
                    break;
                case 2:
                    if (this.chk_float.Checked)
                    {
                        objMod.PreSetFloatReg(Address, float.Parse(this.txt_Value.Text.Trim()));
                    }
                    else
                    {
                        objMod.PreSetKeepReg(Address, ushort.Parse(this.txt_Value.Text.Trim()));                 
                    }
                    break;
                case 3:
                    break;
                default:
                    break;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.lbl_time.Text = DateTime.Now.ToLongTimeString();
        }
    }
}
